/**
 * @file    posix/pthread_mutex.h
 * @brief   
 *
 ****************************************************************************
 * @version $Id: pthread_mutex.h 2628 2010-06-08 23:30:40Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_POSIX_pthread_mutex_H__
 #define __WOSH_Core_POSIX_pthread_mutex_H__

 #include <woshDefs.h>
 #include <pthread.h>


class Mutex_pthread
 {
	public:
		/**
		 * @brief   Empty constructor. Initialize the Mutex
		 */
		Mutex_pthread() {
			pthread_mutex_init( &this->mux, NULL );
		 }

		/**
		 * @brief   Empty destructor. Destroy the Mutex
		 */
		virtual ~Mutex_pthread() {
			pthread_mutex_destroy( &this->mux );
		 }

	public:
		/**
		 * @brief   Lock the Mutex (does not check if Mutex was already locked)
		 * @note    check function pthread_mutex_lock() for more info
		 * @note    check function QMutex::lock() for more info
		 */
		inline void lock() const {
			pthread_mutex_lock( &this->mux );
		 }
		/**
		 * @brief   UnLock the Mutex (does not check if Mutex was locked)
		 * @note    check function pthread_mutex_unlock() for more info
		 * @note    check function QMutex::unlock() for more info
		 */
		inline void unLock() const {
			pthread_mutex_unlock( &this->mux );
		 }
		/**
		 * @brief   Try locking the Mutex and return response
		 * @note    check function pthread_mutex_trylock() for more info
		 */
		inline bool tryLock() const {
			return (bool)pthread_mutex_trylock( &this->mux );
		 }

	private:
		Mutex_pthread( const Mutex_pthread& ) { }
		const Mutex_pthread& operator=( const Mutex_pthread& ) { return *this; }

	protected:
		mutable pthread_mutex_t mux;

}; // class def


class MutexRW_pthread
 {
	public:
		/**
		 * @brief   Empty constructor.
		 */
		MutexRW_pthread()
			: attr(NULL) {
			pthread_rwlock_init(&this->rwLock, this->attr);
		 }
		/**
		 * @brief   Empty destructor.
		 */
		virtual ~MutexRW_pthread() {
			pthread_rwlock_destroy(&this->rwLock);
		 }

	public:
		inline void lockForRead() const {
			pthread_rwlock_rdlock(&this->rwLock);
		 }
		inline void lockForWrite() const {
			pthread_rwlock_wrlock(&this->rwLock);
		 }
		inline void unLock() const {
			pthread_rwlock_unlock(&this->rwLock);
		 }



		inline bool tryLockForRead() const {
			return (bool)pthread_rwlock_tryrdlock( &this->rwLock );
		 }

		inline bool tryLockForWrite() const {
			return (bool)pthread_rwlock_trywrlock( &this->rwLock );
		 }

	private:
		/**
		 * @brief   Empty private copy-constructor.
		 */
		MutexRW_pthread( const MutexRW_pthread& ) { }
		/**
		 * @brief   Empty private assignement-operator.
		 */
		const MutexRW_pthread& operator=( const MutexRW_pthread& ) {
			return *this;
		 }

	protected:
		mutable pthread_rwlock_t rwLock;
		mutable pthread_rwlockattr_t *attr;

}; // class def


 
#endif //__WOSH_Core_POSIX_pthread_mutex_H__
